home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / Ph 1.1.1 / PhClient / glob.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-08  |  15.2 KB  |  502 lines  |  [TEXT/MPS ]

  1. /*______________________________________________________________________
  2.  
  3.     glob.c - Global Variables and Functions.
  4. _____________________________________________________________________*/
  5.  
  6. #pragma load "precompile"
  7. #include "MacTCPCommonTypes.h"
  8. #include "rez.h"
  9. #include "utl.h"
  10. #include "glob.h"
  11. #include "serv.h"
  12. #include "mtcp.h"
  13. #include "oop.h"
  14. #include "rslv.h"
  15.  
  16. /*______________________________________________________________________
  17.  
  18.     Variables.
  19. _____________________________________________________________________*/
  20.  
  21. short                NumMenus;            /* number of menus in menu bar */
  22. MenuHandle        AppleMenu;            /* handle to apple menu */
  23. MenuHandle        FileMenu;            /* handle to file menu */
  24. MenuHandle        EditMenu;            /* handle to edit menu */
  25. MenuHandle        ProxyMenu;            /* handle to proxy menu */
  26. MenuHandle        WindowsMenu;        /* handle to windows menu */
  27. MenuHandle        HelpMenu;            /* handle to help menu */
  28. CursHandle        IBeamHandle;        /* handle to ibeam cursor */
  29. CursHandle        WatchHandle;        /* handle to watch cursor */
  30. CursHandle        BBArray[numBB];    /* array of handles to beachball cursors */
  31. RgnHandle        BigClipRgn;            /* infinite clip region */
  32. Boolean            Done = false;        /* true when time to quit */
  33. short                PhPort;                /* Ph server port number */
  34. short                FontNum;                /* monaco font number */
  35. short                Ascent;                /* font ascent */
  36. short                Descent;                /* font descent */
  37. short                Leading;                /* font leading */
  38. short                LineHeight;            /* font line height */
  39. MenuHandle        SiteMenu = nil;    /* handle to site popup menu */
  40. short                NumSites;            /* number of sites in menu */
  41. Handle            Servers;                /* handle to list of server domain names */
  42. Handle            Domains;                /* handle to list of email domain names */
  43. Boolean            HaveBalloons;        /* true if balloon help available */
  44. Handle            HelpTopics;            /* handle to help topic list, or nil if
  45.                                                 help not available */
  46. Boolean            HelpNative;            /* true if only native help text avail */
  47. Str255            HelpServer;            /* current help server */
  48. short                HelpMenuLength;    /* length of initial part of help menu */
  49. unsigned long    LastSiteUpdate;    /* time of last site list update */
  50. unsigned long    LastHelpUpdate;    /* time of last help menu update */
  51. WindState        TranState;            /* saved transaction window state */
  52. WindState        LoginState;            /* saved login window state */
  53. WindState        NewState;            /* saved new window state */
  54. WindState        OpenState;            /* saved open window state */
  55. WindState        PswdState;            /* saved pswd window state */
  56. WindState        SiteState;            /* saved site window state */
  57. WindState        QueryStates[numPosSave];    /* saved query window states */
  58. WindState        EditStates[numPosSave];        /* saved edit window states */
  59. WindState        HelpStates[numPosSave];        /* saved help window states */
  60. Str255            DefaultServer;        /* default server domain name */
  61. Str255            DbOffMsg = "\p";    /* database turned off error message */
  62.  
  63. /*_____________________________________________________________________
  64.  
  65.     glob_Error - Issue Error Message Alert.
  66.         
  67.     Entry:    rezID = resource id of STR# resource.
  68.                 index = index in STR# resource of error message.
  69.                 p0 = param to plug into error message.
  70. _____________________________________________________________________*/
  71.  
  72. void glob_Error (short rezID, short index, Str255 p0)
  73.  
  74. {
  75.     glob_BringToFront();
  76.     utl_ErrorAlertRez(rezID, index, errorMsgID, 
  77.         oop_ModalUpdate, p0, nil, nil, nil);
  78. }
  79.  
  80. /*_____________________________________________________________________
  81.  
  82.     glob_ErrorCode - Issue MacTCP Error Message Alert.
  83.         
  84.     Entry:    rCode = error code.
  85. _____________________________________________________________________*/
  86.  
  87. void glob_ErrorCode (OSErr rCode)
  88.  
  89. {
  90.     short            i;                /* index in STR# rsrc of error message */
  91.     Str255        p0;            /* parameter to plug into error message */
  92.  
  93.     if (rCode == mtcpCancel) {
  94.         return;
  95.     } else if (rCode == ipBadCnfgErr) {
  96.         i = msgBadCnfg;
  97.     } else if (rCode == rslvNoServers) {
  98.         i = msgNoServers;
  99.     } else if (rCode == badUnitErr || rCode == unitEmptyErr) {
  100.         i = msgBadUnit;
  101.     } else if (rCode == ipDestDeadErr) {
  102.         i = msgDestDead;
  103.     } else if (rCode == authNameErr || rCode == rslvNoSuchDomain) {
  104.         i = msgNoSuchName;
  105.     } else if (rCode == noNameServer) {
  106.         i = msgNoNameServer;
  107.     } else if (rCode == openFailed || rCode == commandTimeout) {
  108.         i = msgOpenFailed;
  109.     } else if (rCode == connectionTerminated || rCode == connectionClosing) {
  110.         if (*DbOffMsg) {
  111.             glob_Error(servErrors, msgDbOff, DbOffMsg);
  112.             return;
  113.         }
  114.         i = msgCTerminated;
  115.     } else if (rCode == noAnsErr || rCode == rslvNoResponse) {
  116.         i = msgNoAnswer;
  117.     } else if (rCode == ipBadAddr) {
  118.         i = msgBadAddr;
  119.     } else if (rCode == cacheFault) {
  120.         i = msgCacheFault;
  121.     } else if (rCode == dnrErr) {
  122.         i = msgDnrErr;
  123.     } else if (rCode == rslvNameSyntaxErr) {
  124.         i = msgDnrSynErr;
  125.     } else {
  126.         i = msgUnexpected;
  127.         NumToString(rCode, p0);
  128.     }
  129.     glob_Error(macTCPErrors, i, p0);
  130. }
  131.  
  132. /*_____________________________________________________________________
  133.  
  134.     glob_ServErrorCode - Issue Server Error Message Alert.
  135.     
  136.     Entry:    sCode = server response code.
  137.                 alias = alias.
  138. _____________________________________________________________________*/
  139.  
  140. void glob_ServErrorCode (short sCode)
  141.  
  142. {
  143.     short        i;                /* index in STR# rsrc of error message */
  144.     Str255    p0;            /* parameter to plug into error message */
  145.     
  146.     if (sCode == phFailReadOnly) {
  147.         i = msgReadOnly;
  148.         serv_GetReadOnlyReason(p0);
  149.     } else if (sCode == phNoMatches) {
  150.         i = msgNoSuchRecord;
  151.     } else if (sCode == phManyMatches) {
  152.         i = msgNameNotUniq;
  153.     } else if (sCode == phIllegalVal) {
  154.         i = msgIllegal;
  155.     } else if (sCode == phPermErr) {
  156.         i = msgBadLogin;
  157.     } else if (sCode == phLockTimeout) {
  158.         i = msgLockTimeout;
  159.     } else if (sCode == phFieldNotThere) {
  160.         i = msgLoginNoAlias;
  161.     } else {
  162.         i = msgCodeErr;
  163.         NumToString(sCode, p0);
  164.     }
  165.     glob_Error(servErrors, i, p0);
  166. }
  167.  
  168. /*_____________________________________________________________________
  169.  
  170.     glob_CheckPswdSel - Check Password Selection
  171.     
  172.     Entry:    theDialog = pointer to movable modal dialog record.
  173.     
  174.     This function checks the current TextEdit field in the dialog, 
  175.     which should be a password field. If the selection range is not
  176.     either the entire password or an insertion point at the end of the
  177.     password, the selection range is reset to be an insertion point at
  178.     the end of the password.
  179. _____________________________________________________________________*/
  180.  
  181. void glob_CheckPswdSel (DialogPtr theDialog)
  182.  
  183. {
  184.     TEHandle            textH;            /* handle to active TextEdit record */
  185.     short                selStart;        /* selection start pos */
  186.     short                selEnd;            /* selection end pos */
  187.     short                nSel;                /* number of chars in selection */
  188.     short                teLength;        /* size of active TextEdit record */
  189.     
  190.     textH = ((DialogPeek)theDialog)->textH;
  191.     selStart = (**textH).selStart;
  192.     selEnd = (**textH).selEnd;
  193.     nSel = selEnd - selStart;
  194.     teLength = (**textH).teLength;
  195.     if ((selStart < teLength || selEnd < teLength) && nSel < teLength) {
  196.         SetPort(theDialog);
  197.         TESetSelect(teLength, teLength, textH);
  198.     }
  199. }
  200.  
  201. /*_____________________________________________________________________
  202.  
  203.     glob_FilterPswdChar - Filter Password Character.
  204.     
  205.     Entry:    theDialog = pointer to movable modal dialog record.
  206.                 key = the character.
  207.                 modifiers = modifiers field from event record.
  208.                 pswd = pointer to password string.
  209.                 
  210.     Exit:        function result = true if character should be processed,
  211.                     false if it should be discarded.
  212.                     
  213.     This function processes keys pressed when a password field is
  214.     active. The actual password string is kept separately, and the field
  215.     displays only bullets.
  216. _____________________________________________________________________*/
  217.  
  218. Boolean glob_FilterPswdChar (DialogPtr theDialog, char key, 
  219.     short modifiers, Str255 pswd)
  220.  
  221. {
  222.     TEHandle            textH;            /* handle to active TextEdit record */
  223.     short                teLength;        /* size of active TextEdit record */
  224.     short                nSel;                /* number of chars in selection */
  225.  
  226.     glob_CheckPswdSel(theDialog);
  227.     textH = ((DialogPeek)theDialog)->textH;
  228.     teLength = (**textH).teLength;
  229.     nSel = (**textH).selEnd - (**textH).selStart;
  230.     if (key == deleteKey) {
  231.         if (nSel) {
  232.             *pswd = 0;
  233.         } else if (*pswd) {
  234.             (*pswd)--;
  235.         }
  236.         return true;
  237.     } else if (utl_StandardAsciiChar(key) && *pswd-nSel < 255) {
  238.         Event.message = '•';
  239.         if (nSel) *pswd = 0;
  240.         (*pswd)++;
  241.         *(pswd + *pswd) = key;
  242.         return true;
  243.     } else if (key == tabKey || key == returnKey || 
  244.         key == enterKey || key == escapeKey ||
  245.         ((modifiers & cmdKey) && key == '.')) {
  246.         return true;
  247.     } else {
  248.         glob_Error(servErrors, msgBadFieldChar, nil);
  249.         return false;
  250.     }
  251. }
  252.  
  253. /*_____________________________________________________________________
  254.  
  255.     glob_FilterPswdEditCmd - Filter Password Edit Command.
  256.     
  257.     Entry:    theDialog = pointer to movable modal dialog record.
  258.                 theItem = menu item.
  259.                 pswd = pointer to password string.
  260.                     
  261.     This function processes the Edit menu commands Copy and Paste when a 
  262.     password field is active.  The actual password string is kept separately, 
  263.     and the field displays only bullets.
  264. _____________________________________________________________________*/
  265.  
  266. void glob_FilterPswdEditCmd (DialogPtr theDialog, short theItem, Str255 pswd)
  267.  
  268. {
  269.     TEHandle            textH;            /* handle to active TextEdit record */
  270.     short                teLength;        /* size of active TextEdit record */
  271.     short                nSel;                /* number of chars in selection */
  272.     Handle            scrap;            /* handle to scrap to be pasted */
  273.     short                scrapLen;        /* length of scrap to be pasted */
  274.     char                *p;                /* pointer to scrap char */
  275.  
  276.     glob_CheckPswdSel(theDialog);
  277.     textH = ((DialogPeek)theDialog)->textH;
  278.     teLength = (**textH).teLength;
  279.     nSel = (**textH).selEnd - (**textH).selStart;
  280.     if (theItem == clearCmd) {
  281.         if (nSel) {
  282.             *pswd = 0;
  283.             oop_DoCommand(theDialog, editID, clearCmd);
  284.         }
  285.     } else if (theItem == pasteCmd) {
  286.         if (!glob_FilterPaste()) return;
  287.         TEFromScrap();
  288.         scrap = TEScrapHandle();
  289.         scrapLen = GetHandleSize(scrap);
  290.         if (scrapLen - nSel > 255) return;
  291.         if (nSel) {
  292.             *pswd = 0;
  293.             TEDelete(textH);
  294.         }
  295.         p = *scrap;
  296.         while (scrapLen--) {
  297.             *(pswd + *pswd + 1) = *p;
  298.             (*pswd)++;
  299.             p++;
  300.             TEKey('•', textH);
  301.         }
  302.     }
  303. }
  304.  
  305. /*_____________________________________________________________________
  306.  
  307.     glob_FilterAsciiChar - Filter Server or Alias/Name Character.
  308.     
  309.     Entry:    theDialog = pointer to movable modal dialog record.
  310.                 key = the character.
  311.                 modifiers = modifiers field from event record.
  312.                 
  313.     Exit:        function result = true if character should be processed,
  314.                     false if it should be discarded.
  315.                     
  316.     This function processes keys pressed when a dialog server or alias/name
  317.     field is active. 
  318. _____________________________________________________________________*/
  319.  
  320. Boolean glob_FilterAsciiChar (DialogPtr theDialog, 
  321.     char key, short modifiers)
  322.  
  323. {
  324.     TEHandle            textH;            /* handle to active TextEdit record */
  325.     short                teLength;        /* size of active TextEdit record */
  326.     short                nSel;                /* number of chars in selection */
  327.  
  328.     textH = ((DialogPeek)theDialog)->textH;
  329.     teLength = (**textH).teLength;
  330.     nSel = (**textH).selEnd - (**textH).selStart;
  331.     if ((utl_StandardAsciiChar(key) && teLength-nSel < 255) ||
  332.         key == tabKey || key == returnKey || key == deleteKey ||
  333.         key == enterKey || key == escapeKey ||
  334.         key == leftArrow || key == rightArrow ||
  335.         ((modifiers & cmdKey) && key == '.')) {
  336.         return true;
  337.     } else {
  338.         glob_Error(servErrors, msgBadFieldChar, nil);
  339.         return false;
  340.     }
  341. }
  342.  
  343. /*_____________________________________________________________________
  344.  
  345.     glob_FilterPaste - Filter Paste Command.
  346.     
  347.     Exit:        function result = true if the scrap contains only
  348.                     standard printable ascii characters.
  349. _____________________________________________________________________*/
  350.  
  351. Boolean glob_FilterPaste (void)
  352.  
  353. {    
  354.     Handle            scrap;        /* handle to scrap to be pasted */
  355.     short                scrapLen;    /* length of scrap to be pasted */
  356.     char                *p;            /* pointer to scrap char */
  357.  
  358.     TEFromScrap();
  359.     scrap = TEScrapHandle();
  360.     scrapLen = GetHandleSize(scrap);
  361.     p = *scrap;
  362.     while (scrapLen--) {
  363.         if (!utl_StandardAsciiChar(*p)) {
  364.             glob_Error(servErrors, msgBadFieldChar, nil);
  365.             return false;
  366.         };
  367.         p++;
  368.     }
  369.     return true;
  370. }
  371.  
  372. /*_____________________________________________________________________
  373.  
  374.     glob_DrawSitePopup - Draw Server Site Popup Menu.
  375.     
  376.     Entry:    rect = rectangle in which to draw the menu.
  377.                 curSel = index in menu of currently selected item.
  378. _____________________________________________________________________*/
  379.  
  380. void glob_DrawSitePopup (Rect *rect, short curSel)
  381.  
  382. {
  383.     Rect            frameRect;            /* framing rect */
  384.     Str255        server;                /* currently selected server name */
  385.     PolyHandle    triangle;            /* handle to triangle polygon */
  386.     GrafPtr        curPort;                /* the grafport */
  387.     short            oldFont;                /* saved font number */
  388.     short            oldSize;                /* saved font size */
  389.     
  390.     GetPort(&curPort);
  391.     oldFont = curPort->txFont;
  392.     oldSize = curPort->txSize;
  393.     TextFont(0);
  394.     TextSize(12);
  395.     frameRect = *rect;
  396.     frameRect.left += 4;
  397.     GetItem(SiteMenu, curSel, server);
  398.     TextBox(server+1, *server, &frameRect, teJustLeft);
  399.     frameRect.left -= 4;
  400.     InsetRect(&frameRect, -1, -1);
  401.     FrameRect(&frameRect);
  402.     MoveTo(frameRect.right, frameRect.top+2);
  403.     LineTo(frameRect.right, frameRect.bottom);
  404.     LineTo(frameRect.left+2, frameRect.bottom);
  405.     triangle = OpenPoly();
  406.     MoveTo(frameRect.right-14, (frameRect.top + frameRect.bottom - 5)>>1);
  407.     Line(10, 0);
  408.     Line(-5, 5);
  409.     Line(-5, -5);
  410.     ClosePoly();
  411.     PaintPoly(triangle);
  412.     KillPoly(triangle);
  413.     TextFont(oldFont);
  414.     TextSize(oldSize);
  415. }
  416.  
  417. /*_____________________________________________________________________
  418.  
  419.     glob_GetSiteIndex - Get Index of Site in Site Menu.
  420.     
  421.     Entry:    server = server domain name.
  422.     
  423.     Exit:     function result = index in Site menu of corresponding site.
  424. _____________________________________________________________________*/
  425.  
  426. short glob_GetSiteIndex (Str255 server)
  427.  
  428. {
  429.     short                    i;                        /* loop index */
  430.     char                    *p;                    /* pointer to server domain name */
  431.     
  432.     p = *Servers;
  433.     for (i = 1; i <= NumSites; i++) {
  434.         if (EqualString(p, server, true, true)) break;
  435.         p += *p+1;
  436.     }
  437.     if (i > NumSites) i = 1;
  438.     return i;
  439. }
  440.  
  441. /*_____________________________________________________________________
  442.  
  443.     glob_PopupSiteMenu - Popup the Site Menu.
  444.     
  445.     Entry:    rect = popup rectangle.
  446.                 oldSel = old selected item.
  447.     
  448.     Exit:     newSel = new selected item, or -1 if none selected.
  449.                 newDomain = new domain name, if newSel != -1.
  450. _____________________________________________________________________*/
  451.  
  452. void glob_PopupSiteMenu (Rect *rect, short oldSel, short *newSel,
  453.     Str255 newDomain)
  454.     
  455. {
  456.     Point            popPoint;            /* location of popup menu, global coords */
  457.     char            *p;                    /* pointer to domain name */
  458.     short            i;                        /* loop index */
  459.     long            result;                /* PopUpMenuSelect result */
  460.     
  461.     CheckItem(SiteMenu, oldSel, true);
  462.     popPoint = *(Point*)rect;
  463.     LocalToGlobal(&popPoint);
  464.     InsertMenu(SiteMenu, -1);
  465.     result = PopUpMenuSelect(SiteMenu, popPoint.v, popPoint.h, oldSel);
  466.     DeleteMenu(popupID);
  467.     CheckItem(SiteMenu, oldSel, false);
  468.     if ((result >> 16) & 0xffff) {
  469.         *newSel = result & 0xffff;
  470.         InvalRect(rect);
  471.         i = *newSel;
  472.         p = *Servers;
  473.         while (--i) p += *p+1;
  474.         utl_CopyPString(newDomain, p);
  475.     } else {
  476.         *newSel = -1;
  477.     }
  478. }
  479.  
  480. /*_____________________________________________________________________
  481.  
  482.     glob_BringToFront - Bring program to front.
  483. _____________________________________________________________________*/
  484.  
  485. void glob_BringToFront (void)
  486.  
  487. {
  488.     NMRec            nRec;            /* Notification manger record */
  489.     
  490.     if (oop_InForeground()) return;
  491.     nRec.qType = nmType;
  492.     nRec.nmMark = 1;
  493.     nRec.nmIcon = GetResource('SICN', phSmallIconID);
  494.     nRec.nmSound = nil;
  495.     nRec.nmStr = nil;
  496.     nRec.nmResp = nil;
  497.     NMInstall(&nRec);
  498.     while (!oop_InForeground()) oop_DoEvent(nil, everyEvent, longSleep, nil);
  499.     NMRemove(&nRec);
  500.     oop_UpdateAll();
  501. }
  502.